package com.tianzj.mvc.leave.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.mvc.act.controller.ActProcessController;
import com.tianzj.mvc.leave.pojo.OaLeave;
import com.tianzj.mvc.leave.service.LeaveService;
import com.tianzj.mvc.system.controller.BaseController;
import com.tianzj.util.AjaxJson;
import com.tianzj.util.DateUtils;
import com.tianzj.util.JsonUtil;
import com.tianzj.util.Page;

@RestController
@RequestMapping("leave")
public class LeaveController{
	
	private Logger logger  =  LoggerFactory.getLogger(LeaveController.class );
	
	@Autowired
	private LeaveService leaveService;
	
	@RequestMapping("getLeave")
	public ModelAndView getLeave(){
		return new ModelAndView("leave/leaveList");
	}
	
	@RequestMapping("getLeaveList")
	public Object getLeaveList(OaLeave leave,Page page){
		List<OaLeave> leaves = leaveService.getLeaveList(page,leave);
		return JsonUtil.list2BootStrapJson(leaves);
	}
	
	@PostMapping("addLeave")
	public ModelAndView addLeave(OaLeave leave,HttpServletRequest request){
		try {
			String startTime = request.getParameter("startTimeStr");
			String endTime = request.getParameter("endTimeStr");
			if(StringUtils.isNotBlank(startTime)){
				Date start = DateUtils.parse(startTime);
				leave.setStartTime(start.getTime());
			}
			if(StringUtils.isNotBlank(endTime)){
				Date end = DateUtils.parse(endTime);
				leave.setEndTime(end.getTime());
			}
			leaveService.addLeave(leave);
		} catch (Exception e) {
			logger.error("addLeave:"+e.getMessage());
		}
		return new ModelAndView("redirect:/leave/getLeave");
	}
	
	@PostMapping("startLeaveAct")
	public AjaxJson startLeaveAct(String id){
		try {
			leaveService.startLeaveAct(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AjaxJson.success();
	}
}
